<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <!--<el-form-item label="交易金额" prop="sum">
        <el-input
          v-model="queryParams.sum"
          placeholder="请输入交易金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="交易类型" prop="dealType">
        <el-select v-model="queryParams.dealType" placeholder="请选择" clearable size="small">
          <el-option
            v-for="item in optionjylx"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!--<el-form-item label="剩余金额" prop="balance">
        <el-input
          v-model="queryParams.balance"
          placeholder="请输入剩余金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!--<el-form-item label="是否为待结算" prop="isLock">
        <el-select v-model="queryParams.isLock" placeholder="请选择" clearable size="small">
          <el-option
            v-for="item in optionsfwdjs"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table  height="600" style="width: 100%" v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center"  />-->
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <!-- <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
         <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->

      <!--<el-table-column label="业务id" align="center" prop="linkId" width="180" show-overflow-tooltip />-->
      <el-table-column label="交易金额" align="center" prop="sum" show-overflow-tooltip />
      <el-table-column label="交易类型" align="center" prop="dealType" >
        <template slot-scope="scope">
          <span v-show="scope.row.dealType==1">支出</span>
          <span v-show="scope.row.dealType==2">收入</span>
        </template>
      </el-table-column>
      <el-table-column label="用户id" align="center" prop="userId"  show-overflow-tooltip />
      <el-table-column label="用户名称" align="center" prop="realName"  show-overflow-tooltip />

      <!--<el-table-column label="剩余金额" align="center" prop="balance" width="100" show-overflow-tooltip/>-->
      <!--<el-table-column label="是否为待结算" align="center" prop="isLock" width="100" >
        <template slot-scope="scope">
          <span v-show="scope.row.isLock==0">正常</span>
          <span v-show="scope.row.isLock==1">待结算</span>
        </template>
      </el-table-column>-->
      <!--<el-table-column label="备注" align="center" prop="remark" width="180" show-overflow-tooltip/>-->

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流水对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="用户名称" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="备注" prop="mk">
          <el-input v-model="form.mk" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="剩余金额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入剩余金额" />
        </el-form-item>
        <el-form-item label="是否为待结算  0正常 1待结算" prop="isLock">
          <el-input v-model="form.isLock" placeholder="请输入是否为待结算  0正常 1待结算" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBill, getBill, delBill, addBill, updateBill, exportBill } from  "@/api/lanbangkeji/user/liushuishuju";

export default {
  name: "Bill",
  components: {
  },
  data() {
    return {
      // 0正常 1待结算
      optionsfwdjs:[
        {value:0,label:'正常'},
        {value:1,label:'待结算'},
      ],
      //交易类型 1支出 2收入
      optionjylx:[
        {value:1,label:'支出'},
        {value:2,label:'收入'},
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流水表格数据
      billList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sum: null,
        dealType: null,
        userId: null,
        realName: null,
        mk: null,
        balance: null,
        isLock: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询流水列表 */
    getList() {
      this.loading = true;
      listBill(this.queryParams).then(response => {
        this.billList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        billId: null,
        createTime: null,
        updateTime: null,
        remark: null,
        linkId: null,
        sum: null,
        dealType: null,
        userId: null,
        realName: null,
        mk: null,
        balance: null,
        isLock: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.billId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加流水";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const billId = row.billId || this.ids
      getBill(billId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流水";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.billId != null) {
            updateBill(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBill(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.billId || this.ids;
      this.$confirm('是否确认删除流水编号为"' + billIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delBill(billIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有流水数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportBill(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
